﻿using Bot.Domain.Bot;
using Bot.Domain;
using System.Collections.Generic;
using System.Threading.Tasks;
using Bot.Domain.BotImport;

using Bot.Domain;
using Bot.Domain;



using Bot.Domain.Entities;
using System;
using Bot.Domain.Enum;

namespace Bot.Domain
{
    public interface IBotApiService
    {
        Task<string> CreateBot(BotDto bot);
        Task<bool> DeleteBot(BotDto bot);
        Task<ApiResult> TrainBot(BotDto bot);
        Task<ApiResult> GetBot(BotDto bot);
        Task<ApiResult> RestoreBot(BotDto bot, BotImportDataQueue botImportDataQueue);
        //intent       
        Task<ApiResult> ImportBot(BotDto bot, BotImportDataQueue botImportDataQueue);
        Task<ApiResult> GetOperations(BotDto bot, string operationName);
        Task<ApiResult> ExportBot(BotDto bot);
        Task<string> CreateIntent(IntentDto intent, BotDto bot);
        Task<string> EditIntent(IntentDto intent, BotDto bot, string MappingToBotEngine);
        Task DeleteIntent(BotDto bot, string MappingToBotEngine);
        Task<ApiResult> BatchDeleteIntents(BotDto bot, string[] MappingToBotEngines);

        //Entity

        Task<string> CreateEntity(BotDto bot, EntityDto obj);
        Task<string> EditEntity(BotDto bot, EntityDto obj);
        Task DeleteEntity(BotDto bot, EntityBasicDto obj);
        Task BatchUpdateIntents(IntentDto[] intents, BotDto bot);
        Task<List<IntentModelAggregate>> GetIntentList(BotDto bot);
        Task<DetectIntentResponseDto> ChatWithBot(BotDto bot, QueryIntentParameter queryIntentParameter, string type = "question", string intentName = "");
        Task<ApiResult> BatchDeleteEntities(BotDto bot, string[] MappingToBotEngines);
        Task<List<EntityModelAggregate>> GetEntityList(BotDto bot);
        Task BatchUpdateEntities(EntityDto[] entities, BotDto bot);

        Task<ApiResult> IdentifyEntity(BotDto bot, QueryIntentParameter queryIntentParameter, List<QuestionEntity> intentQuestionEntities);
    }

    public class BotApiService : IBotApiService
    {
        private IComm100BotApiService _comm100BotApiService;
        private IComm100OwnBotApiService _comm100OwnBotApiService;

        public BotApiService(IComm100BotApiService comm100BotApiService,
            IComm100OwnBotApiService comm100OwnBotApiService)
        {
            _comm100BotApiService = comm100BotApiService;
            _comm100OwnBotApiService = comm100OwnBotApiService;
        }

        public async Task<ApiResult> BatchDeleteEntities(BotDto bot, string[] MappingToBotEngines)
        {
            ApiResult apiResult = new ApiResult { IsSuccess = false, };
            if (bot.Engine == EnumBotType.comm100Bot.ToString())            
            {
                apiResult = await _comm100BotApiService.BatchDeleteEntities(bot, MappingToBotEngines);
            }
            if (bot.Engine == EnumBotType.comm100OwnBot.ToString())
            {
                apiResult = await _comm100OwnBotApiService.BatchDeleteEntities(bot, MappingToBotEngines);
            }
            return apiResult;
        }

        public async Task<ApiResult> BatchDeleteIntents(BotDto bot, string[] MappingToBotEngines)
        {
            ApiResult apiResult = new ApiResult { IsSuccess = false, };
            if (bot.Engine == EnumBotType.comm100Bot.ToString())
            {
                apiResult = await _comm100BotApiService.BatchDeleteIntents(bot, MappingToBotEngines);
            }
            if (bot.Engine == EnumBotType.comm100OwnBot.ToString())
            {
                apiResult = await _comm100OwnBotApiService.BatchDeleteIntents(bot, MappingToBotEngines);
            }
            return apiResult;
        }

        public async Task BatchUpdateEntities(EntityDto[] entities, BotDto bot)
        {
            if (bot.Engine == EnumBotType.comm100Bot.ToString())
            {
                await _comm100BotApiService.BatchUpdateEntities(entities, bot);
            }
            if (bot.Engine == EnumBotType.comm100OwnBot.ToString())
            {
                await _comm100OwnBotApiService.BatchUpdateEntities(entities, bot);
            }
        }

        public async Task BatchUpdateIntents(IntentDto[] intents, BotDto bot)
        {
            if (bot.Engine == EnumBotType.comm100Bot.ToString())
            {
                await _comm100BotApiService.BatchUpdateIntents(intents, bot);
            }
            if (bot.Engine == EnumBotType.comm100OwnBot.ToString())
            {
                await _comm100OwnBotApiService.BatchUpdateIntents(intents, bot);
            }
        }

        public async Task<DetectIntentResponseDto> ChatWithBot(BotDto bot, QueryIntentParameter queryIntentParameter, string type = "question", string intentName = "")
        {
            DetectIntentResponseDto detectIntentResponseDto = new DetectIntentResponseDto();
            if (bot.Engine == EnumBotType.comm100Bot.ToString())
            {
                detectIntentResponseDto = await _comm100BotApiService.ChatWithBot(bot, queryIntentParameter, type, intentName);
            }
            if (bot.Engine == EnumBotType.comm100OwnBot.ToString())
            {
                detectIntentResponseDto = await _comm100OwnBotApiService.ChatWithBot(bot, queryIntentParameter, type, intentName);
            }
            return detectIntentResponseDto;
        }

        public async Task<string> CreateBot(BotDto bot)
        {
            string strResult = "";
            if (bot.Engine == EnumBotType.comm100Bot.ToString())
            {
                strResult = await _comm100BotApiService.CreateBot(bot);
            }
            if (bot.Engine == EnumBotType.comm100OwnBot.ToString())
            {
                strResult = await _comm100OwnBotApiService.CreateBot(bot);
            }
            return strResult;
        }

        public async Task<string> CreateEntity(BotDto bot, EntityDto obj)
        {
            string strResult = "";
            if (bot.Engine == EnumBotType.comm100Bot.ToString())
            {
                strResult = await _comm100BotApiService.CreateEntity(bot, obj);
            }
            if (bot.Engine == EnumBotType.comm100OwnBot.ToString())
            {
                strResult = await _comm100OwnBotApiService.CreateEntity(bot, obj);
            }
            return strResult;
        }

        public async Task<string> CreateIntent(IntentDto intent, BotDto bot)
        {
            string strResult = "";
            if (bot.Engine == EnumBotType.comm100Bot.ToString())
            {
                strResult = await _comm100BotApiService.CreateIntent(intent, bot);
            }
            if (bot.Engine == EnumBotType.comm100OwnBot.ToString())
            {
                strResult = await _comm100OwnBotApiService.CreateIntent(intent, bot);
            }
            return strResult;
        }

        public async Task<bool> DeleteBot(BotDto bot)
        {
            bool bResult = false;
            if (bot.Engine == EnumBotType.comm100Bot.ToString())
            {
                bResult = await _comm100BotApiService.DeleteBot(bot);
            }
            if (bot.Engine == EnumBotType.comm100OwnBot.ToString())
            {
                bResult = await _comm100OwnBotApiService.DeleteBot(bot);
            }
            return bResult;
        }

        public async Task DeleteEntity(BotDto bot, EntityBasicDto obj)
        {
            if (bot.Engine == EnumBotType.comm100Bot.ToString())
            {
                await _comm100BotApiService.DeleteEntity(bot, obj);
            }
            if (bot.Engine == EnumBotType.comm100OwnBot.ToString())
            {
                await _comm100OwnBotApiService.DeleteEntity(bot, obj);
            }
        }

        public async Task DeleteIntent(BotDto bot, string MappingToBotEngine)
        {
            if (bot.Engine == EnumBotType.comm100Bot.ToString())
            {
                await _comm100BotApiService.DeleteIntent(bot, MappingToBotEngine);
            }
            if (bot.Engine == EnumBotType.comm100OwnBot.ToString())
            {
                await _comm100OwnBotApiService.DeleteIntent(bot, MappingToBotEngine);
            }
        }

        public async Task<string> EditEntity(BotDto bot, EntityDto obj)
        {
            string strResult = "";
            if (bot.Engine == EnumBotType.comm100Bot.ToString())
            {
                strResult = await _comm100BotApiService.EditEntity(bot, obj);
            }
            if (bot.Engine == EnumBotType.comm100OwnBot.ToString())
            {
                strResult = await _comm100OwnBotApiService.EditEntity(bot, obj);
            }
            return strResult;
        }

        public async Task<string> EditIntent(IntentDto intent, BotDto bot, string MappingToBotEngine)
        {
            string strResult = "";
            if (bot.Engine == EnumBotType.comm100Bot.ToString())
            {
                strResult = await _comm100BotApiService.EditIntent(intent, bot, MappingToBotEngine);
            }
            if (bot.Engine == EnumBotType.comm100OwnBot.ToString())
            {
                strResult = await _comm100OwnBotApiService.EditIntent(intent, bot, MappingToBotEngine);
            }
            return strResult;
        }

        public async Task<ApiResult> ExportBot(BotDto bot)
        {
            ApiResult apiResult = new ApiResult { IsSuccess = false, };
            if (bot.Engine == EnumBotType.comm100Bot.ToString())
            {
                apiResult = await _comm100BotApiService.ExportBot(bot);
            }
            if (bot.Engine == EnumBotType.comm100OwnBot.ToString())
            {
                apiResult = await _comm100OwnBotApiService.ExportBot(bot);
            }
            return apiResult;
        }

        public async Task<ApiResult> GetBot(BotDto bot)
        {
            ApiResult apiResult = new ApiResult { IsSuccess = false, };
            if (bot.Engine == EnumBotType.comm100Bot.ToString())
            {
                apiResult = await _comm100BotApiService.GetBot(bot);
            }
            if (bot.Engine == EnumBotType.comm100OwnBot.ToString())
            {
                apiResult = await _comm100OwnBotApiService.GetBot(bot);
            }
            return apiResult;
        }

        public async Task<List<EntityModelAggregate>> GetEntityList(BotDto bot)
        {
            List<EntityModelAggregate> entityModelAggregates = new List<EntityModelAggregate>();
            if (bot.Engine == EnumBotType.comm100Bot.ToString())
            {
                entityModelAggregates = await _comm100BotApiService.GetEntityList(bot);
            }
            if (bot.Engine == EnumBotType.comm100OwnBot.ToString())
            {
                entityModelAggregates = await _comm100OwnBotApiService.GetEntityList(bot);
            }
            return entityModelAggregates;
        }

        public async Task<List<IntentModelAggregate>> GetIntentList(BotDto bot)
        {
            List<IntentModelAggregate> intentModelAggregates = new List<IntentModelAggregate>();
            if (bot.Engine == EnumBotType.comm100Bot.ToString())
            {
                intentModelAggregates = await _comm100BotApiService.GetIntentList(bot);
            }
            if (bot.Engine == EnumBotType.comm100OwnBot.ToString())
            {
                intentModelAggregates = await _comm100OwnBotApiService.GetIntentList(bot);
            }
            return intentModelAggregates;
        }

        public async Task<ApiResult> GetOperations(BotDto bot, string operationName)
        {  
            ApiResult apiResult = new ApiResult { IsSuccess = false, };
            if (bot.Engine == EnumBotType.comm100Bot.ToString())
            {
                apiResult = await _comm100BotApiService.GetOperations(bot, operationName);
            }
            if (bot.Engine == EnumBotType.comm100OwnBot.ToString())
            {
                apiResult = await _comm100OwnBotApiService.GetOperations(bot, operationName);
            }
            return apiResult;
        }

        public async Task<ApiResult> IdentifyEntity(BotDto bot, QueryIntentParameter queryIntentParameter, List<QuestionEntity> intentQuestionEntities)
        {
            ApiResult apiResult = new ApiResult { IsSuccess = false, };
            if (bot.Engine == EnumBotType.comm100Bot.ToString())
            {

            }
            if (bot.Engine == EnumBotType.comm100OwnBot.ToString())
            {
                apiResult = await _comm100OwnBotApiService.IdentifyEntity(bot, queryIntentParameter, intentQuestionEntities);
            }
            return apiResult;
        }

        public async Task<ApiResult> ImportBot(BotDto bot, BotImportDataQueue botImportDataQueue)
        {
            ApiResult apiResult = new ApiResult { IsSuccess = false, };
            if (bot.Engine == EnumBotType.comm100Bot.ToString())
            {
                apiResult = await _comm100BotApiService.ImportBot(bot, botImportDataQueue);
            }
            if (bot.Engine == EnumBotType.comm100OwnBot.ToString())
            {
                apiResult = await _comm100OwnBotApiService.ImportBot(bot, botImportDataQueue);
            }
            return apiResult;
        }

        public async Task<ApiResult> RestoreBot(BotDto bot, BotImportDataQueue botImportDataQueue)
        {
            ApiResult apiResult = new ApiResult { IsSuccess = false, };
            if (bot.Engine == EnumBotType.comm100Bot.ToString())
            {
                apiResult = await _comm100BotApiService.RestoreBot(bot, botImportDataQueue);
            }
            if (bot.Engine == EnumBotType.comm100OwnBot.ToString())
            {
                apiResult = await _comm100OwnBotApiService.RestoreBot(bot, botImportDataQueue);
            }
            return apiResult;
        }

        public async Task<ApiResult> TrainBot(BotDto bot)
        {
            ApiResult apiResult = new ApiResult { IsSuccess = false, };
            if (bot.Engine == EnumBotType.comm100Bot.ToString())
            {
                apiResult = await _comm100BotApiService.TrainBot(bot);
            }
            if (bot.Engine == EnumBotType.comm100OwnBot.ToString())
            {
                apiResult = await _comm100OwnBotApiService.TrainBot(bot);
            }
            return apiResult;
        }
    }
}
